/**
 * Created by Administrator on 2015/11/29.
 */


/**
 * Created by thanatos on 15-9-7.
 */

(function ($, angular) {

    var app = angular.module('app', [])

    /*--catalog--*/
    app.controller('catalog', ['$scope', '$http', function ($scope, $http) {
        $scope.getCategories = function () {
            $http.get('/recipe/categories').success(function (resp) {
                $scope.categories = resp.categories
            })
                .error(function (err) {
                    console.log('there are a error in /main/catalog')
                })
        }
        $scope.over = function ($event) {
            console.log('over')
            var $target = $($event.target)
            if(!$target.attr('data-index'))
                return
            $('.hover').removeClass('hover')
            $scope.index = $target.attr('data-index')
            $('li[data-index="'+$scope.index+'"]').addClass('hover')
            $('.hidden-catalog').addClass('hover')
            if($scope.categories){
                console.log('hoverIndex>>>', $scope.cname)
                $scope.children = $scope.categories[$scope.index].children
                console.log('children >>>')
                console.log($scope.children)
            }
        }
        $scope.leave = function ($event) {
            var $target = $($event.target)
            $('.hover').removeClass('hover')
        }
    }])

    app.controller('recipe$details', ['$scope', '$http', function($scope, $http){
        $scope.collect = function (rid, $event) {
            var $target = $($event.target)
            $http.post('/user/private/recipe/'+rid+'/collect').success(function (resp) {
                if(resp.code == 0){
                    $scope.collect_count += 1
                    $target.text('已收藏('+$scope.collect_count+')')
                    alert(resp.message)
                }else{
                    alert(resp.message)
                }
            }).error(function (err) {
                alert(err.message || '收藏失败')
            })
        }
        $scope.upvote = function (rid, $event) {
            var $target = $($event.target)
            $http.post('/user/private/recipe/'+rid+'/upvote').success(function (resp) {
                if(resp.code == 0){
                    $scope.upvote_count += 1
                    $target.text('已点赞('+$scope.upvote_count+')')
                    alert(resp.message)
                }else{
                    alert(resp.message)
                }
            }).error(function (err) {
                alert(err.message || '点赞失败')
            })
        }
    }])

    /*--suggest retrieval--*/
    app.controller('retrieval', ['$scope', '$http', function ($scope, $http) {

        //���ύ
        $scope.submit = function (field) {
            console.log("field is ", field)
            if(field){
                $scope.qr = field
            }
            if(!$scope.qr || !$scope.qr.replace('/\s+/g', '')) return;
            document.forms['searcher'].elements['qr'].value = $scope.qr
            document.forms['searcher'].submit()
        }

        //������ʾ
        $scope.suggest = function () {
            console.log('value is ', $scope.qr)
            if(!$scope.qr){
                $scope.show = false
            }else{
                $http.get('/suggest', {params:{field:$scope.qr}})
                    .success(function (resp) {
                        console.log('suggests value is ', resp)
                        if(resp.suggests.length==0){
                            $scope.show=false
                        }
                        else{
                            $scope.show = true
                            $scope.suggests = resp.suggests
                        }
                    })
                    .error(function (resp) {
                        console.log('-=-=-=-=-=-=-')
                    })
            }
        }
    }])

    app.controller('person_header', ['$scope', '$http', function ($scope, $http) {
        $scope.consider = function (nick, $event) {
            console.log('nick is ' + nick)
            var $target = $($event.target)
            $http.post('/user/private/'+nick+'/consider').success(function (resp) {
                alert(resp.message)
                if(resp.code == 200){
                    $target.text('已关注')
                    $target.attr('disable', 'true')
                }
            }).error(function (err) {
                alert(err.message || "关注失败")
                    console.log(err)
            })
        }
    }])

    app.controller('recipe_details', ['$scope', '$http', function($scope, $http){
        $scope.addToMenu = function (nick) {
            $http.get('/user/private/'+nick+'/menu/list').success(function (resp) {
                if(resp.code != 200){
                    alert(resp.message||'未知错误')
                    return
                }
                $scope.menus = resp.menus
                $('#addToMenuModal').modal()
            }).error(function (err) {
                alert(err.message || "未知错误")
            })
        }
        $scope.postToSave = function () {
            if(!$scope.mid) return
            var form = document.forms['athena']
            $http.post(form.action, {mid:+$scope.mid, rid:$scope.rid}).success(function (resp) {
                if(resp.code == 200){
                    $('#addToMenuModal').modal('hide')
                    alert(resp.message || '添加菜单成功')
                }else{
                    alert(resp.message || '添加菜单失败')
                }

            }).error(function (err) {
                alert(err.message || "未知错误")
            })
        }

    }])

})(jQuery, angular)